<script setup>
import {ref} from "vue"

const numberPriceToChinesePrice = (n) => {
  if(typeof n === 'number'){
    n = n.toString()
  }
  if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n)) {
    chinesePrice.value = '数据非法';
    return
  }
  let unit = '千百拾亿千百拾万千百拾元角分';
  let str = '';
  n = n.replace(',', '');
  n += '00';
  const p = n.indexOf('.');
  if (p >= 0) {
    n = n.substring(0, p) + n.substr(p + 1, 2);
  }
  unit = unit.substr(unit.length - n.length);
  for (let i = 0; i < n.length; i++) {
    str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);
  }
  chinesePrice.value = str
      .replace(/零(千|百|拾|角)/g, '零')
      .replace(/(零)+/g, '零')
      .replace(/零(万|亿|元)/g, '$1')
      .replace(/(亿)万|壹(拾)/g, '$1$2')
      .replace(/^元零?|零分/g, '')
      .replace(/元$/g, '元整');
};

// console.log(numberPriceToChinesePrice("213456"))
const number = ref(0)
const chinesePrice = ref("")
</script>

<template>
<div>
    <el-input-number :precision="2" v-model="number" placeholder="请输入数字" @input="numberPriceToChinesePrice"/>
    <div class="mt-5">结果：{{chinesePrice}}</div>
</div>
</template>

<style scoped>

</style>